home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
163_01
/
cc11.c
< prev
next >
Wrap
Text File
|
1991-01-05
|
13KB
|
463 lines
/*
** execution begins here
*/
#ifdef CMD_LINE
main(argc, argv) int argc, *argv; {
argcs=argc;
argvs=argv;
#else
main() {
#endif
sout("Small-C Version 2.0 for the IBM PC -- Release 1.01\n", stderr);
sout("Portions Copyright 1982 by J. E. Hendrix\n", stderr);
sout("Converted to the IBM PC by D. R. Hicks\n", stderr);
sout("\n", stderr);
#ifdef DYNAMIC
swnext=malloc(SWTABSZ);
swend=swnext+((SWTABSZ-SWSIZ)>>1);
stage=malloc(STAGESIZE);
stagelast=stage+STAGELIMIT;
wq=malloc(WQTABSZ*BPW);
litq=malloc(LITABSZ);
#ifdef HASH
macn=malloc(MACNSIZE);
cptr=macn-1;
while(++cptr < MACNEND) *cptr=0;
#endif
macq=malloc(MACQSIZE);
pline=malloc(LINESIZE);
mline=malloc(LINESIZE);
#else
swend=(swnext=swq)+SWTABSZ-SWSIZ;
stagelast=stage+STAGELIMIT;
#endif
swactive= /* not in switch */
stagenext= /* direct output mode */
iflevel= /* #if... nesting level =0 */
skiplevel= /* #if... not encountered */
macptr= /* clear the macro pool */
csp = /* stack ptr (relative) */
errcnt= /* number of errors flagged */
errflag=0; /* not skipping errors till ";" */
eof= /* not eof yet */
ncmp= /* not in compound statement */
dmode= /* not in DATASEG */
files=
filearg=
nxtlab=
quote[1]=0;
ccode=1; /* enable preprocessing */
wqptr=wq; /* clear while queue */
quote[0]='"'; /* fake a quote literal */
input=input2=EOF;
ask(); /* get user options */
sout("\n", stderr); /* blank line to separate options from listing */
kill(); /* reset input buffer prior to first read */
preprocess(); /* fetch first line */
#ifdef DYNAMIC
#ifdef HASH
symtab=malloc(NUMLOCS*SYMAVG + NUMGLBS*SYMMAX);
#else
symtab=malloc(NUMLOCS*SYMAVG);
/* global space is allocated with each new entry */
#endif
#endif
#ifdef HASH
cptr=STARTGLB-1;
while(++cptr < ENDGLB) *cptr=0;
#endif
glbptr=STARTGLB;
glbflag=1;
header(); /* intro code */
setops(); /* set values in op arrays */
parse(); /* process ALL values */
outside(); /* verify outside any function */
externs(); /* declare all externals to the assembler */
trailer(); /* follow-up code */
if(output != stdout) fclose(output);
summary(); /* print error summary */
}
/*
** process all input text
**
** At this level, only static declarations.
** defines, includes and function
** definitions are legal ...
*/
parse() {
while (eof==0) {
if(amatch("extern", 6)) dodeclare(EXTERNAL);
else if(amatch("static", 6)) dodeclare(STATIC);
else if(dodeclare(PUBLIC)) ;
else if(match("#asm")) doasm();
else if(match("#include")) doinclude();
else if(match("#define")) addmac();
else newfunc(PUBLIC);
blanks(); /* force eof if pending */
}
}
/*
** dump the literal pool
*/
dumplits(size) int size; {
int j, k;
k=0;
while (k<litptr) {
defstorage(size);
j=10;
while(j--) {
outdec(getint(litq+k, size));
k=k+size;
if ((j==0) | (k>=litptr)) {
nl();
break;
}
outbyte(',');
}
}
}
/*
** dump zeroes for default initial values
*/
dumpzero(size, count) int size, count; {
int j;
while (count > 0) {
defstorage(size);
count = genzeros(count); /* generate literal zeros */
}
}
/*
** verify compile ends outside any function
*/
outside() {
if (ncmp) error("no closing bracket");
}
/*
** get run options
*/
ask() {
int defin, defout, deflist, defi, defm, defa, defp;
int dftin, dftout, dftlist;
char inname[81], outname[81], listname[81];
#ifdef CMD_LINE
int argnum, truth;
char *ptr;
#endif
defin = defout = deflist = defi = defm = defa = defp = 0;
dftin = dftout = dftlist = 1;
ctext = monitor = alarm = pause = 1;
input = stdin;
output = stdout;
listfp=0;
strcpy(inname, "con.C");
strcpy(outname, "con.ASM");
strcpy(listname, "NUL.LST");
#ifdef CMD_LINE
argnum = 1;
while(argnum < argcs) {
ptr = argvs[argnum++]; /* cast arg as char * */
if(*ptr == ';') { /* if defaults to be taken */
defin = defout = deflist = defi = defm = defa = defp = 1;
break;
}
else if(*ptr != '-') { /* if file name */
if(!defin) {
if(*ptr != ',') {
joinname(inname, ptr);
dupename(outname, inname);
dftin = 0;
dftout = 0;
}
defin = 1;
}
else if(!defout) {
if(*ptr != ',') {
joinname(outname, ptr);
dftout = 0;
}
defout = 1;
}
else if(!deflist) {
if(*ptr != ',') {
joinname(listname, ptr);
dftlist = 0;
}
deflist = 1;
}
else {
fprintf(stderr, "ERROR: Extraneous parameter %s\n", ptr);
errcnt++;
}
}
else { /* must be option */
ptr++; /* skip "-" */
truth = 1;
if(upper(*ptr) == 'N') {
truth = 0;
ptr++;
}
switch(upper(*ptr)) {
case 'I': { /* interleave C source */
ctext = truth;
defi = 1;
break;
}
case 'M': { /* monitor function headers */
monitor = truth;
defm = 1;
break;
}
case 'A': { /* sound alarm on errors */
alarm = truth;
defa = 1;
break;
}
case 'P': { /* pause on errors */
pause = truth;
defp = 1;
break;
}
default:
fprintf(stderr, "ERROR: Extraneous option -%c\n", *ptr);
errcnt++;
}
}
}
#endif
line=mline; /* use macro buffer for kbd buffer */
if(!defin) {
fprintf(stdout, "Input filename [%s]: ", inname);
if(prompt("", line, LINESIZE)) {
joinname(inname, line);
dupename(outname, inname);
dftin = 0;
}
}
if(!defout) {
fprintf(stdout, "Output filename [%s]: ", outname);
if(prompt("", line, LINESIZE)) {
joinname(outname, line);
dftout = 0;
}
}
if(!deflist) {
fprintf(stdout, "Listing filename [%s]: ", listname);
if(prompt("", line, LINESIZE)) {
joinname(listname, line);
dftlist = 0;
}
}
while(!defi) {
prompt("Interleave C source? ", line, LINESIZE);
if(upper(*line)=='Y') ctext=YES;
else if(upper(*line)!='N') continue;
defi = 1;
}
if((!defm) && dftout) { /* don't monitor if output to con */
monitor = 0;
defm = 1;
}
while(!defm) {
prompt("Monitor function headers? ", line, LINESIZE);
if(upper(*line)=='Y') monitor=YES;
else if(upper(*line)!='N') continue;
defm = 1;
}
while(!defa) {
prompt("Sound alarm on errors? ", line, LINESIZE);
if(upper(*line)=='Y') alarm=YES;
else if(upper(*line)!='N') continue;
defa = 1;
}
while(!defp) {
prompt("Pause on errors? ", line, LINESIZE);
if(upper(*line)=='Y') pause=YES;
else if(upper(*line)!='N') continue;
defp = 1;
}
if(!dftin) {
input=fopen(inname,"r");
if(!input) {
fprintf(stderr, "ERROR: Cannot open %s for input\n", inname);
exit(errno);
}
}
if(!dftout) {
output=fopen(outname,"w");
if(!output) {
fprintf(stderr, "ERROR: Cannot open %s for output\n", outname);
exit(errno);
}
}
if(!dftlist) {
listfp=fopen(listname,"w");
if(!listfp) {
fprintf(stderr, "